﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Space_Cleaner.SpaceCleaner
{
    abstract class SC_AbstractScriptEvent
    {
        private TimeSpan start;
        private LinkedList<SC_IScriptEventListener> listeners;

        public SC_AbstractScriptEvent(long _startInMS)
        {
            this.start = new TimeSpan(_startInMS * 10000);
        }

        public void addListener(SC_IScriptEventListener _listener)
        {
            if (this.listeners == null)
                this.listeners = new LinkedList<SC_IScriptEventListener>();
            this.listeners.AddLast(_listener);
        }

        public TimeSpan getStartTime()
        {
            return this.start;
        }

        public Boolean update(GameTime _gameTime, SC_World _world, ContentManager _contentMgr)
        {
            if (this.listeners != null)
                foreach (SC_IScriptEventListener listener in listeners)
                    listener.onUpdate(_gameTime, this.start - _gameTime.TotalGameTime);

            if (start < _gameTime.TotalGameTime)
            {
                run(_world, _contentMgr);
                return true;
            }
            return false;
        }

        public void run(SC_World world, ContentManager contentMgr)
        {
            if (this.listeners != null)
                foreach(SC_IScriptEventListener listener in listeners)
                    listener.onRun();
            onRun(world, contentMgr);
        }

        public abstract void onRun(SC_World world, ContentManager contentMgr);
    }
}
